<template>
  <div class="playlist" ref="playlistPadding">
    <!-- Better-scoll+长列表渲染 -->
    <div class="wrapper" ref="wrapper">
      <div class="list">
        <div class="playlist-top">
          <div class="left">
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-bofang3"></use>
            </svg>
            <div class="text">
              <div class="title">播放全部</div>
              <div class="num">（共{{ playlist.tracks.length }}首）</div>
            </div>
          </div>
          <div class="btn">
            + 收藏（{{ changeValue(playlist.subscribedCount) }}）
          </div>
        </div>
        <RecycleScroller
          :items="playlist.tracks"
          :item-height="130"
          :item-size="52"
          key-field="id"
          v-slot="{ item, index }"
        >
          <div class="playItem" @click="playMusicing(index)">
            <div class="left">
              <div class="index">{{ index + 1 }}</div>
              <div class="content">
                <div class="title">{{ item.name }}</div>
                <div class="anthor">
                  <span class="name">{{ item.ar[0].name }}</span>
                </div>
              </div>
              <div class="right">
                <i class="iconfont icon-24gl-playSquare"></i>
                <i class="iconfont icon-liebiao"></i>
              </div>
            </div>
          </div>
        </RecycleScroller>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from "@better-scroll/core";
import { mapMutations } from "vuex";
import bus from "@/utils/eventBus.js";
import listViewTop from "@/components/listview/listViewTop.vue";
import { getPlayListDetail } from "../api/index.js";
export default {
  props: ["playlist"],
  data() {
    return {
      playList: [],
    };
  },
  components: {
    listViewTop,
  },
  created() {
    this.getPlayLists(this.$route.query.id);
  },
  methods: {
    ...mapMutations(["setPlayIndex", "setSumTime", "setShow"]),
    //评论数修改
    changeValue(num) {
      let res = 0;
      if (num >= 100000000) {
        res = num / 100000000;
        res = res.toFixed(2) + "亿";
      } else if (num > 10000) {
        res = num / 10000;
        res = res.toFixed(2) + "万";
      }
      return res;
    },
    //播放音乐
    playMusicing(i) {
      //点击每个列表时都会直接切换歌曲
      this.setPlayIndex(i);
      //播放歌曲
      bus.emit("isPlay", false);
      //展示歌曲控件
      this.setShow(true);
      this.$refs.playlistPadding.value.style.paddingBottom = "1.2rem";
      //切歌时把时间更新了
      this.setSumTime(this.$store.state.playlist[i].dt);
      //获取歌词
      this.getLyricList();
    },
    //切换歌词
    getLyricList(lyric) {
      this.$store.dispatch("reqLyric", {
        id: store.state.playlist[store.state.playCurrentIndex].id,
      });
    },
    //获取推荐歌单数据
    async getPlayLists(id) {
      const { data: res } = await getPlayListDetail(id, 30);
      this.playList = res.playlist;
      // state.playList.playCount=playCount('number',state.playList.playCount)
      // 把歌曲列表放到vuex中
      this.$store.commit("setPlaylist", this.playList.tracks);
    },
  },
};
</script>

<style lang="less" scoped>
.listViewTop {
  margin: 0 auto;
}
.playlist {
  width: 7.5rem;
  background-color: #fff;
  border-top-left-radius: 0.3rem;
  border-top-right-radius: 0.3rem;
  .wrapper {
    overflow: hidden;
    .list {
      padding: 0 0.25rem;
      .playlist-top {
        display: flex;
        justify-content: space-between;
        height: 1rem;
        align-items: center;
        .left {
          display: flex;
          align-items: center;

          .iconfont {
            font-size: 0.6rem;
          }

          .title {
            font-size: 0.34rem;
            font-weight: 900;
          }

          .num {
            font-size: 0.24rem;
            color: #666;
          }

          .text {
            display: flex;
            align-items: center;
            margin-left: 0.2rem;
          }
        }
        .btn {
          font-size: 0.24rem;
          color: #fff;
          background-color: orangered;
          line-height: 0.6rem;
          padding: 0.1rem;
          border-radius: 0.4rem;
        }
      }
      .playItem {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 1rem;
        .left {
          flex: 1;
          display: flex;
          align-items: center;
          color: #666;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          word-break: break-all;
          word-wrap: break-word;

          .index {
            width: 0.4rem;
          }

          .content {
            padding-left: 0.4rem;
            width: 4rem;
          }

          .title {
            width: calc(100% - 0.4rem);
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
            word-break: break-all;
            word-wrap: break-word;
            font-size: 0.3rem;
            font-weight: 900;
            color: #000;
            margin-bottom: 0.1rem;
          }

          .tag {
            font-size: 0.2rem;
            color: orangered;
            border: 1px solid orangered;
            border-radius: 0.1rem;
            margin-right: 0.1rem;
          }

          .anthor {
            color: #666;
            display: flex;
            overflow: hidden;
            height: 0.35rem;

            .name {
              width: 3rem;
            }
          }
        }

        .right {
          width: 1.6rem;
          margin-left: 1rem;
          .iconfont {
            margin: 0 0.2rem;
          }
        }
      }
    }
  }
}
</style>
